package com.shop.controller;

import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.shop.entity.ShopProductTemplate;
import com.shop.service.ShopProductTemplateService;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.List;

/**
 * 控制层。
 *
 * @author 彭新才
 * @since 2024-10-12
 */
@RestController
@Tag(name = "商品模板接口")
@RequestMapping("/shopProductTemplate")
public class ShopProductTemplateController {

    @Autowired
    private ShopProductTemplateService shopProductTemplateService;

    /**
     * 添加。
     *
     * @param shopProductTemplate
     * @return {@code true} 添加成功，{@code false} 添加失败
     */
    @PostMapping("save")
    @Operation(summary = "保存")
    public boolean save(@RequestBody @Parameter(name = "商品模板") ShopProductTemplate shopProductTemplate) {
        return shopProductTemplateService.add(shopProductTemplate);
    }

    /**
     * 根据主键删除。
     *
     * @param id 主键
     * @return {@code true} 删除成功，{@code false} 删除失败
     */
    @DeleteMapping("remove/{id}")
    @Operation(summary = "根据主键删除")
    public boolean remove(@PathVariable @Parameter(name = "主键") Serializable id) {
        return shopProductTemplateService.removeById(id);
    }

    /**
     * 根据主键更新。
     *
     * @param shopProductTemplate
     * @return {@code true} 更新成功，{@code false} 更新失败
     */
    @PutMapping("update")
    @Operation(summary = "根据主键更新")
    public boolean update(@RequestBody @Parameter(name = "主键") ShopProductTemplate shopProductTemplate) {
        return shopProductTemplateService.updateById(shopProductTemplate);
    }

    /**
     * 查询所有。
     *
     * @return 所有数据
     */
    @GetMapping("list")
    @Operation(summary = "查询所有")
    public List<ShopProductTemplate> list() {
        return shopProductTemplateService.list();
    }

    /**
     * 根据主键获取详细信息。
     *
     * @param id 主键
     * @return 详情
     */
    @GetMapping("getInfo")
    @Operation(summary = "根据主键获取")
    public ShopProductTemplate getInfo(@Parameter(name = "主键") @RequestParam("id") Serializable id) {
        return shopProductTemplateService.getById(id);
    }

    /**
     * 分页查询。
     *
     * @param page 分页对象
     * @return 分页对象
     */
    @GetMapping("page")
    @Operation(summary = "分页查询")
    public Page<ShopProductTemplate> page(@Parameter(name = "分页信息") Page<ShopProductTemplate> page, ShopProductTemplate shopProductTemplate) {
        QueryWrapper query = new QueryWrapper();
        if (shopProductTemplate.getId() != null) {
            query.eq(ShopProductTemplate::getId, shopProductTemplate.getId());
        }
        if (shopProductTemplate.getContent() != null && shopProductTemplate.getContent() != "") {
            query.like(ShopProductTemplate::getContent, shopProductTemplate.getContent());
        }
        if (shopProductTemplate.getCreateTime() != null) {
            query.eq(ShopProductTemplate::getCreateTime, shopProductTemplate.getCreateTime());
        }
        if (shopProductTemplate.getUpdateTime() != null) {
            query.eq(ShopProductTemplate::getUpdateTime, shopProductTemplate.getUpdateTime());
        }
        if (shopProductTemplate.getCreateUserId() != null) {
            query.eq(ShopProductTemplate::getCreateUserId, shopProductTemplate.getCreateUserId());
        }
        if (shopProductTemplate.getUpdateUserId() != null) {
            query.eq(ShopProductTemplate::getUpdateUserId, shopProductTemplate.getUpdateUserId());
        }
        return shopProductTemplateService.page(page, query);
    }

}
